package com.example.exporttemplate.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.example.exporttemplate.domain.Person;
import com.example.exporttemplate.easyexcelHandler.HeaderHandler;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 阿里巴巴easyexcel导出service
 *
 * @author liulindong
 * @version 2023/12/19 20:19
 */
@Service
public class EasyExcelExportService {


    public void setResponseHeader(HttpServletResponse response, String fileName) {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + UriUtils.encode(fileName, "UTF-8"));
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
    }


    /**
     * 简单的注解导出
     */
    public void simpleAnnotationExport(HttpServletResponse response) {
        String fileName = "注解方式简单导出.xlsx";
        this.setResponseHeader(response, fileName);
        try {
            EasyExcel.write(response.getOutputStream(), Person.class)
                    .sheet("注解方式简单导出sheet")
                    .registerWriteHandler(HeaderHandler.getHeaderStyle())
                    .doWrite(this::getDataList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取数据
     */
    private List<Person> getDataList() {
        List<Person> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Person person = new Person();
            person.setNo(i + 1);
            person.setName("姓名" + i);
            person.setSex("性别" + i);
            person.setAge(i);
            list.add(person);
        }
        return list;
    }


    private List<List<String>> head() {
        List<List<String>> list = ListUtils.newArrayList();
        List<String> head0 = ListUtils.newArrayList();
        head0.add("序号");
        List<String> head1 = ListUtils.newArrayList();
        head1.add("姓名");
        List<String> head2 = ListUtils.newArrayList();
        head2.add("性别");
        List<String> head3 = ListUtils.newArrayList();
        head3.add("年龄");

        list.add(head0);
        list.add(head1);
        list.add(head2);
        list.add(head3);
        return list;
    }



    /**
     * 通过模板导出excel
     */
    public void templateExport(HttpServletResponse response) {
        String fileName = "模板方式导出.xlsx";
        this.setResponseHeader(response, fileName);
        // 获取模板文件
        InputStream template = this.getClass().getClassLoader().getResourceAsStream("static" + File.separator + "exportTemplate.xlsx");
        try(ExcelWriter writer = EasyExcel.write(response.getOutputStream()).withTemplate(template).build()) {
            // 填充列表数据
            WriteSheet sheet = EasyExcel.writerSheet().build();
            writer.fill(getDataList(), sheet);

            // 填充其他数据
            Map<String, Object> otherData = new HashMap<>();
            otherData.put("title", "模板方式导出");
            otherData.put("time", "2020-12-19");
            writer.fill(otherData, sheet);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }




    /**
     * 无实体类的导出
     */
    public void noEntityExport(HttpServletResponse response) {
        this.setResponseHeader(response, "无实体类方式导出.xlsx");
        try {
            EasyExcel.write(response.getOutputStream()).sheet("无实体类方式导出sheet").head(head()).doWrite(getDataList());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
